package top.jiangqiang.qianyi.common.properties;

import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import top.jiangqiang.qianyi.common.constants.GlobalConstants;
import top.jiangqiang.qianyi.common.filter.caching.wrapper.CachingHttpServletRequestWrapper;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ConfigurationProperties(prefix = GlobalConstants.GLOBAL_PREFIX + ".filter")
@Data
public class FilterProperties {
    private CachingProperties caching = new CachingProperties();
    private LoggingProperties logging = new LoggingProperties();
    private XssProperties xss = new XssProperties();

    @Data
    public static class CachingProperties {

        /**
         * caching过滤器拦截路径
         */
        private List<String> includePatterns = List.of(GlobalConstants.CommonRequestURLConstants.API_PREFIX + "/*");
        /**
         * caching过滤器优先级
         */
        private int order = Ordered.HIGHEST_PRECEDENCE + 100;
        /**
         * caching过滤器排除的路径
         */
        private List<String> excludePatterns = new ArrayList<>();

        /**
         * 最大Body缓存长度，超过此长度的body不支持作为日志打印
         */
        private int maxCacheBodyLength = CachingHttpServletRequestWrapper.DEFAULT_MAX_CACHE_LIMIT;
    }

    @Data
    public static class LoggingProperties {
        /**
         * 启用请求日志打印
         */
        private Boolean enabled = true;
        /**
         * caching过滤器拦截路径
         */
        private List<String> includePatterns = List.of(GlobalConstants.CommonRequestURLConstants.API_PREFIX + "/*");
        /**
         * caching过滤器优先级
         */
        private int order = Ordered.HIGHEST_PRECEDENCE + 200;
        /**
         * caching过滤器排除的路径
         */
        private List<String> excludePatterns = new ArrayList<>();
        /**
         * 日志打印级别
         */
        private LogLevel level = LogLevel.ALL;

        /**
         * 如果该请求体支持打印，但是超过此长度，则只截取部分进行打印
         */
        private int maxPrintBodyLength = 128;


        @RequiredArgsConstructor
        @Getter
        public enum LogLevel {
            BASIC("只打印URL，查询参数和响应状态码等基础信息"),
            BASIC_HEADER("打印基础信息以及请求头"),
            BASIC_BODY("打印基础信息以及请求体"),
            ALL("打印基础信息，请求头以及请求体");
            private final String desc;
        }

    }

    @Data
    public static class XssProperties {

        /**
         * caching过滤器拦截路径
         */
        private List<String> includePatterns = List.of(GlobalConstants.CommonRequestURLConstants.API_PREFIX + "/*");
        /**
         * caching过滤器优先级
         */
        private int order = Ordered.HIGHEST_PRECEDENCE + 300;
        /**
         * caching过滤器排除的路径
         */
        private List<String> excludePatterns = new ArrayList<>();

    }
}
